package com.dmm;

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class redismain {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.79.100",6379);
        jedis.auth("dmm145811");
        //ping 检查是否连接
        String ping = jedis.ping();
        System.out.println("ping的值"+ping);
        //常用命令
        System.out.println("查询"+jedis.keys("*"));
        String set = jedis.set("名字", "zhangsan");
        System.out.println(set);
        System.out.println(jedis.get("名字"));
        System.out.println(jedis.append("名字", "和李四"));
        System.out.println(jedis.get("名字"));
        System.out.println(jedis.strlen("名字"));
        jedis.setnx("id","13");
        System.out.println(jedis.keys("*"));
        System.out.println(jedis.get("id"));
        //加一操作
        jedis.set("age","10");
        jedis.incr("age");
        System.out.println(jedis.get("age"));
        //自定义步长相加操作
        jedis.incrBy("age",256);
        System.out.println(jedis.get("age"));

        //减一操作
        jedis.decr("age");
        System.out.println(jedis.get("age"));
        //自定义步长相减操做
        jedis.decrBy("age",256);
        System.out.println(jedis.get("age"));

        //设置多个key-value
        jedis.mset("name","zhamhsan","email","1458119489@qq.com");
        System.out.println(jedis.keys("*"));

        //获取多个value存储在list里
        System.out.println(jedis.mget("name","email"));

        //同时设置一个或多个 key-value 对，当且仅当所有给定 key 都不存在。
        jedis.msetnx("name","lisi");
        System.out.println(jedis.get("name"));

        //获得值的范围，类似java中的substring，前包，后包
        System.out.println(jedis.getrange("name", 1, 3));


        //用 <value>  覆写<key>所储存的字符串值，从<起始位置>开始(索引从0开始)。
        jedis.setrange("name",2,"haha");
        System.out.println(jedis.get("name"));

        //设置键值的同时，设置过期时间，单位秒。
        jedis.setex("men",10,"mente");
        System.out.println(jedis.ttl("men"));
        jedis.close();

    }



    @Test
    public void test(){
        Jedis jedis = new Jedis("192.168.79.100",6379);
        jedis.auth("dmm145811");
        System.out.println(jedis.ttl("men"));

        //以新换旧，设置了新值同时获得旧值。
        System.out.println(jedis.getSet("name", "makabaka"));
        System.out.println(jedis.get("name"));
        jedis.close();
    }

    /**
     * 列表(List)
     */
    @Test
    public void test1(){
        Jedis jedis = new Jedis("192.168.79.100",6379);
        jedis.auth("dmm145811");
        //lpush/rpush  <key><value1><value2><value3> .... 从左边/右边插入一个或多个值。
        jedis.lpush("lss","a1","a2","a3");
        jedis.rpush("rss","b1","b2","b3");
        //lpop/rpop  <key>从左边/右边吐出一个值。值在键在，值光键亡。
        System.out.println(jedis.lpop("lss"));
        System.out.println(jedis.rpop("rss"));


        //rpoplpush  <key1><key2>从<key1>列表右边吐出一个值，插到<key2>列表左边。
        System.out.println(jedis.rpoplpush("lss","rss"));
        System.out.println(jedis.get("rss"));
        jedis.close();
    }

    @Test
    public void test3(){
        Jedis jedis = new Jedis("192.168.79.100",6379);
        jedis.auth("dmm145811");
        System.out.println(jedis.hkeys("USER"));
        System.out.println(jedis.hvals("USER"));
        Set<String> user = jedis.hkeys("USER");
        System.out.println(user);
        List<String> user1 = jedis.hvals("USER");
        System.out.println(user1);
    }

    @Test
    public void test4(){
        Jedis jedis = new Jedis("192.168.79.100",6379);
        jedis.auth("dmm145811");
        System.out.println(jedis.getrange("EmpService:FindAll", 0, 20));

    }


    @Test
    public void test5(){
        String name ="123456";
        test6(name);

    }

    @Test
    public void test6(Object obj){

        System.out.println(obj.getClass());

        System.out.println(obj.getClass().toString());

    }


}
